package controllers.manage.orderSource;

import controllers.BaseController;
import controllers.auth.Secure;
import helper.GlobalConfig;
import jodd.util.StringUtil;
import me.chanjar.weixin.common.util.StringUtils;
import models.constants.DeletedStatus;
import order.OrderSource;
import play.modules.paginate.JPAExtPaginator;
import play.mvc.Controller;
import play.mvc.With;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by youliangcheng on 17/6/12.
 */
@With(BaseController.class)
public class OrderSourceCtrl extends Controller {

    public static void loadList(Integer pageNumber, String name, String code) {

        pageNumber = pageNumber == null ? 1 : pageNumber;
        Map<String, Object> searchMap = new HashMap<>();

        if (StringUtil.isNotBlank(name)) {
            searchMap.put("name", "%"+name+"%");
        }

        if (StringUtil.isNotBlank(code)) {
            searchMap.put("code", "%"+code+"%");
        }

        JPAExtPaginator<OrderSource> resultPage = OrderSource.findByCondition(searchMap, "id desc", pageNumber, GlobalConfig.PAGE_SIZE);

        render(pageNumber, name, code, resultPage);
    }

    //删除
    public static void deletedSource(long id){
        Map<String , Object> resultMap = new HashMap<>();
        OrderSource orderSource = OrderSource.findBySourceId(id);
        if(orderSource == null){
            resultMap.put("success",false);
            resultMap.put("msg","平台数据不存在,可能已经被删除!");
            renderJSON(resultMap);
        }
        //判断是否可以删除,判断订单是否含有该平台的订单
        if(OrderSource.checkIsUsed(id)){
            resultMap.put("success",false);
            resultMap.put("msg","平台 "+orderSource.name+" 正在被使用,无法删除!");
            renderJSON(resultMap);
        }

        orderSource.deleted = DeletedStatus.DELETED;
        orderSource.save();

        resultMap.put("success",true);
        resultMap.put("msg","删除成功");
        renderJSON(resultMap);
    }

    //详情页
    public static void loadDetail(long id , Integer pageNumber){
        OrderSource orderSource = new OrderSource();
        if(id > 0)
            orderSource = OrderSource.findBySourceId(id);
        render(id , orderSource , pageNumber);

    }

    //保存平台信息
    public static void saveDetail(OrderSource orderSource , Integer pageNumber){

        String url = "/manage/orderSource/OrderSourceCtrl/loadList";
        if(orderSource == null || StringUtils.isBlank(orderSource.name)){
            flash.put("msg","名称不能不为空!");
            redirect(url);
        }

        Secure.setBaseInfo(orderSource);
        if(orderSource.id == null){
            orderSource.loadKey();
            orderSource.deleted = DeletedStatus.UN_DELETED;
            orderSource.save();

        }else {
            orderSource.update();
            url += "/"+pageNumber;
        }

        flash.put("msg","保存成功!");
        redirect(url);

    }

    public static void createOrder(long id ,Integer pageNumber){
        OrderSource orderSource = OrderSource.findBySourceId(id);
        render(orderSource , pageNumber);
    }
}
